#include "ADURoad.h"

ADURoad::ADURoad()
{
	listeners = new CCArray();
}
ADURoad::~ADURoad()
{
	delete listeners;
}

ADURoad* ADURoad::CreateRoadByFileName(const char* tmxFileName) {
	ADURoad* road = new ADURoad();
	if (road && road->initWithTMXFile(tmxFileName)) {
		road->autorelease();
		return road;
	}

	CC_SAFE_DELETE(road);
	return NULL;
}

void ADURoad::setBackground(CCTMXLayer* bg)
{
	m_background = bg;
}

CCTMXLayer* ADURoad::getBackground()
{
	return m_background;
}

void ADURoad::setMeta(CCTMXLayer* mt)
{
	m_meta = mt;
}

CCTMXLayer* ADURoad::getMeta()
{
	return m_meta;
}


void ADURoad::setSPeed(int speed){
	m_speed = speed;
	notifySpeedChanged();
}
int ADURoad::getSpeed(){
	return m_speed;
}

void ADURoad::startRun(){
	CCMoveBy* move = CCMoveBy::create(1, ccp(0, (-1)*m_speed));
	CCRepeatForever* repeat = CCRepeatForever::create(move);
	CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();

	float delta = (-1) * 40 * 64 + visibleSize.height + 64;

	if (this->getPosition().y == 0) {
		this->runAction(repeat);
	} else if (this->getPosition().y < delta) {
		this->stopAllActions();
		this->setPosition(ccp(0, 0));
		this->runAction(repeat);
	}
}

void ADURoad::addListener(ADUMovable* m){
	listeners->addObject(m);
}

void ADURoad::removeListener(ADUMovable* m){
	listeners->removeObject(m);
}

void ADURoad::notifySpeedChanged(){
	CCObject* it;
		CCARRAY_FOREACH(listeners, it) {
			ADUMovable *l = static_cast<ADUMovable*>(it);
			l->onSpeedchanged(m_speed);
		}
}
